package cn.tedu.csmall.product.service.impl;


import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.rmi.CORBA.Util;
import java.util.Date;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {
    @Autowired
    private AlbumMapper mapper;
    @Autowired
    private PictureMapper pictureMappermapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;


    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理添加相册业务");
        String name = albumAddNewDTO.getName();
        log.info("尝试添加的相册名：{}", name);
        int count = mapper.countByName(name);
        log.info("数据库中查到的该相册名称数量：{}", count);
        if (count > 0) {
            String message = "添加失败，尝试添加的相册名已被占用";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_CONFICT, message);
        }
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDTO, album);
        log.debug("准备插入相册数据：{}", album);
        mapper.insert(album);

    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理根据Id删除相册，Id:{}",id);
        AlbumStandardVO album= mapper.getStandardById(id);
        if (album == null){
            String message = "删除失败，该相册不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        {
            int count = pictureMappermapper.countByAlbumId(id);
            if (count != 0){
                String message = "删除失败，尝试访问的相册存在关联图片";
                throw new ServiceException(ServiceCode.ERR_CONFICT,message);
            }
        }
        {
            int count = skuMapper.countByAlbum(id);
            if (count != 0){
                String message = "删除失败，尝试访问的相册存在关联的sku";
                throw new ServiceException(ServiceCode.ERR_CONFICT,message);
            }
        }
        {
            int count = spuMapper.countByAlbum(id);
            if (count != 0){
                String message = "删除失败，尝试访问的相册存在关联的spu数据";
                throw new ServiceException(ServiceCode.ERR_CONFICT,message);
            }
        }
        mapper.deleteById(id);

    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理修改相册详情业务，id:{}数据：{}",id,albumUpdateDTO);
        String name = albumUpdateDTO.getName();
        AlbumStandardVO albumStandardVO = mapper.getStandardById(id);
        if (albumStandardVO==null){
            String message = "修改相册详情失败，尝试访问的相册数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int count = mapper.countByNameAndNotId(name,id);
        if (count > 0) {
            String message = "修改相册详情失败，新的相册名已被占用";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_CONFICT, message);
        }
        Album album = new Album();
        album.setId(id);
        BeanUtils.copyProperties(albumUpdateDTO,album);
        log.debug("开始执行修改相册详情业务，id:{},数据：{}",id,albumUpdateDTO);
        mapper.update(album);
    }

    @Override
    public AlbumStandardVO getStandardById(Long id) {
        AlbumStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO==null){
            String message = "该相册不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardVO;
    }

}
